import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/user.vue'
import ItemCat from '../components/items/ItemCat.vue'
import ItemCatParams from '../components/items/ItemCatParam.vue'
Vue.use(VueRouter)
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home , redirect: "/welcome",children:[
    { path: '/welcome', component: Welcome},
    { path: '/user', component: User},
    { path: '/itemcat', component: ItemCat},
    { path: '/itemcatparam', component: ItemCatParams}
  ]}
]

const router = new VueRouter({
  routes
})
//to要访问的请求路径
router.beforeEach((to,from,next)=>{
  if(to.path==="/login"){
    return next()

  }else{
    let token =window.sessionStorage.getItem("token")
    if(!token)
    {
      next('/login')
    }
    next()
  }


})
export default router
